package com.gxc.blogm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.gxc.authcore.system.util.CurrentUtil;
import com.gxc.blogm.domain.Comment;
import com.gxc.blogm.service.CommentService;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;
import com.gxc.webcore.util.JsonUtil;
import com.gxc.webcore.util.RequestUtil;
import com.gxc.webcore.util.StringUtil;

public class CommentController {

	CommentService commentService;

	public CommentService getCommentService() {
		return commentService;
	}

	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}

	/**
	 * 列表页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("blogm/comment/comment_list.vm");
	}

	/**
	 * 列出一页评论
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ModelAndView grid(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map map = RequestUtil.getInMap(request);
		PageInfo pageInfo = PageInfo.getFromRequest(request);
		PageList pageList = commentService.listPage(map, pageInfo);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", pageList);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 新建评论页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView create(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("blogm/comment/comment_create.vm");
	}

	/**
	 * 新建评论保存
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView createSave(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map map = new HashMap();
		Comment comment = (Comment) RequestUtil.getInObject(request,
				Comment.class);
		comment.setId("comment_" + StringUtil.getUUID());
		comment.setCreate_user(CurrentUtil.getAuthUser().getId());
		comment.setCreate_time(new Date());
		int i = commentService.create(comment);
		map.put("status", "success");
		map.put("data", i);
		String result = JsonUtil.object2json(map);
		response.getWriter().write(result);
		return null;
	}

	/**
	 * 删除一个评论
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		String id = request.getParameter("id");
		int i = commentService.delete(id);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 删除多个评论
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView deleteBatch(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		String id = request.getParameter("id");
		String[] id_array = id.split(",");
		int i = commentService.delete(id_array);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 查看评论
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView view(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		Comment comment = commentService.retrieve(id);
		Map model = new HashMap();
		model.put("comment", comment);
		return new ModelAndView("blogm/comment/comment_view.vm", model);
	}

}
